import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)

// 阻止router重复点击报错问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location) {
  return originalPush.call(this, location).catch(err => err)
}


// 懒加载方式引入

// 登录界面
const Login = () => import('views/login/login.vue')
const Register = () => import('views/login/register.vue')
const ForgotPassword = () => import('views/login/forgotPassword')
const Home = () => import('views/Home.vue')
const homePage = () => import('views/homePage.vue')
// 校区管理
const SchoolManage = () => import('views/schoolManage/schoolManage.vue')
const CampusManage = () => import('views/schoolManage/children/campusManage.vue')
const BuildingManage = () => import('views/schoolManage/children/buildingManage.vue')
const RoomManage = () => import('views/schoolManage/children/roomManage.vue')
//用电管理
const Electricity = () => import ('views/electricityManage/electricityManage.vue')
const ElectricityManage = () => import('views/electricityManage/children/electricityInfo.vue')
const PayDetail = () => import('views/electricityManage/children/payDetail.vue')
const PayInManage = () => import('views/electricityManage/children/payInManage.vue')
const ElectricityUserManage = () =>import('views/electricityManage/children/userManage.vue')
//设备管理
const Equipment = () => import('views/equipmentManage/equipmentManage.vue')
const CenterManage = () => import('views/equipmentManage/children/centerManage.vue')
const EquipErrorLog = () => import('views/equipmentManage/children/equipErrorLog.vue')
const EquipmentManage = () => import('views/equipmentManage/children/equipmentManage.vue')
const EquipModelManage = () => import('views/equipmentManage/children/equipModelManage.vue')
//系统管理
const SystemManage = () => import('views/systemManage/systemManage.vue')
const OrganizationManage = () => import('views/systemManage/children/organizationManage.vue')
const RoleManage = () => import('views/systemManage/children/roleManage.vue')
const UserManage = () => import('views/systemManage/children/userManage.vue')
//账号管理
const AccountManage = () => import('views/accountManage/userManage.vue')
const ChangePw = () => import('views/accountManage/children/changePw.vue')
const UserInfo = () => import('views/accountManage/children/userInfo.vue')


const routes = [
  {
    path: '/',
    redirect: {
      name: 'Login'
    }
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/register',
    name: 'Register',
    component: Register
  },
  {
    path: '/forgotPassword',
    name:'ForgotPassword',
    component:ForgotPassword
  },
  {
    path: '/index',
    component: Home,
    children: [
      {
        path:'',
        name:'首页',
        component:homePage
      },
      {
        path: '/school',
        name: 'SchoolManage',
        component: SchoolManage,
        children:[
          {
            path: 'campusManage',
            name: '校区管理',
            component: CampusManage
          },
          {
            path: 'buildingManage',
            name: '楼栋管理',
            component: BuildingManage
          },
          {
            path: 'roomManage',
            name: '房间管理',
            component: RoomManage
          },
        ]
      },
      {
        path: '/electricity',
        name: 'Electricity',
        component: Electricity,
        children:[
          {
            path: 'electricityManage',
            name: '用电管理',
            component: ElectricityManage,
          },
          {
            path: 'payDetail',
            name: '缴费详情',
            component: PayDetail,
          },
          {
            path: 'payInManage',
            name: '充值管理',
            component: PayInManage,
          },
          {
            path: 'ElUserManage',
            name:'用电用户管理',
            component:ElectricityUserManage
          }
        ]
      },
      {
        path: '/equipment',
        name: 'Equipment',
        component: Equipment,
        children:[
          {
            path: 'centerManage',
            name: '集控管理',
            component: CenterManage,
          },
          {
            path: 'equipErrorLog',
            name: '设备报警日志',
            component: EquipErrorLog,
          },
          {
            path: 'equipmentManage',
            name: '设备管理',
            component: EquipmentManage,
          },
          {
            path: 'equipModelManage',
            name: '设备型号管理',
            component: EquipModelManage,
          },
        ]
      },
      {
        path: '/SystemManage',
        name: 'SystemManage',
        component: SystemManage,
        children:[
          {
            path: 'organizationManage',
            name: '组织管理',
            component: OrganizationManage,
          },
          {
            path: 'roleManage',
            name: '角色管理',
            component: RoleManage,
          },
          {
            path: 'userManage',
            name: '用户管理',
            component: UserManage,
          },
        ]
      },
      {
        path: '/accountManage',
        name: 'AccountManage',
        component: AccountManage,
        children:[
          {
            path: 'changePw',
            name: '修改密码',
            component: ChangePw,
          },
          {
            path: 'userInfo',
            name: '用户信息',
            component: UserInfo,
          },
        ]
      }
    ]
  },
]

const router = new VueRouter({
  mode: 'history',
  // mode:'hash',
  routes
})
router.beforeEach((to, from, next) => {

  if(!window.sessionStorage.getItem('Authorization') && to.name != 'Login') {
    next({name: 'Login'})
  }
  next()
})

export default router
